package tianrun.ziguan.api.calculate.config.service.common.edit;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.dto.request.common.DeleteBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.AddOrEditBody;
import tianrun.ziguan.api.calculate.config.dto.request.common.add.edit.SaveBody;
import tianrun.ziguan.api.calculate.config.service.common.CommonService;
import tianrun.ziguan.api.calculate.config.service.ge.TGeTableConfigService;
import tianrun.ziguan.api.calculate.config.service.template.instance.ITStaticPropertiesInstanceService;
import tianrun.ziguan.api.calculate.config.utils.MapUtil;
import tianrun.ziguan.api.calculate.config.utils.StringUtil;
import tianrun.ziguan.api.common.util.ListUtil;
import tianrun.ziguan.api.common.util.SpringContextUtil;

import java.util.List;
import java.util.Map;

@Service
public class StaticPropertiesModuleDealService extends AbstractDealService {

    private final ITStaticPropertiesInstanceService staticPropertiesInstanceService;

    public StaticPropertiesModuleDealService(TGeTableConfigService tableConfigService,
                                             ITStaticPropertiesInstanceService staticPropertiesInstanceService) {
        super(tableConfigService);
        this.staticPropertiesInstanceService = staticPropertiesInstanceService;
    }

    @Override
    public void before(SaveBody body) {

    }


    @Override
    public String tableName() {
        return "t_static_properties_module";
    }

    @Override
    public void after(SaveBody body) {
        List<JSONObject> instanceData = MapUtil.getList(body.getRawData(), "instance_data", JSONObject.class);
        String templateId = MapUtil.getString(body.getData(), "template_id");
        if (ListUtil.isEmpty(instanceData)||StringUtil.isNullOrBlank(templateId)) return;
        CommonService commonService = SpringContextUtil.getBean(CommonService.class);
        StringBuilder sb=new StringBuilder();
        instanceData.stream().map(jsonObject -> {
            jsonObject.put("module_id",body.getId());
            jsonObject.put("template_id",templateId);
            AddOrEditBody addOrEditBody = new AddOrEditBody();
            addOrEditBody.setTableName("t_static_properties_instance");
            addOrEditBody.setData(jsonObject);
            return addOrEditBody;
        }).forEach(addOrEditBody -> {
            try {
                commonService.addOrEdit(addOrEditBody);
            } catch (IllegalArgumentException e) {
               sb.append(e.getMessage()).append(StringPool.NEWLINE);
            }
        });
        String message = sb.toString();
        Assert.isTrue(message.isEmpty(),message.trim());
    }

    @Override
    public void delBefore(DeleteBody body) {

    }

    @Override
    public void listAfter(List<Map<String, Object>> result) {

    }

    @Override
    public void delAfter(DeleteBody body) {

    }

    @Override
    public void infoAfter(Map<String, Object> bean) {

    }


}
